package com.example.administrator.pomelostreet.pinpai.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.pomelostreet.R;
import com.example.administrator.pomelostreet.pinpai.bean.PinPaiBean;

import java.util.List;

/**
 * Created by yq on 2017/2/7.
 */

public class PinPaiRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<PinPaiBean> mList;
    //接口回调 view
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    public PinPaiRecyclerViewAdapter(Context mContext, List<PinPaiBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_pinpai, parent, false);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            PinPaiBean pinPaiBean = mList.get(position);
            Glide.with(mContext).load(mList.get(position).getPicture()).into(((MyViewHolder) holder).iv_pinpai);

            ((MyViewHolder) holder).tv_one.setText(mList.get(position).getSub_title());
            ((MyViewHolder) holder).tv_two.setText(mList.get(position).getSub_title());
            ((MyViewHolder) holder).tv_three.setText(mList.get(position).getName());
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView iv_pinpai;
        private TextView tv_one, tv_two, tv_three;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_pinpai = (ImageView) itemView.findViewById(R.id.pinpai_iv);
            tv_one = (TextView) itemView.findViewById(R.id.pinpai_textjian);
            tv_two = (TextView) itemView.findViewById(R.id.pinpai_textViewday);
            tv_three = (TextView) itemView.findViewById(R.id.pinpai_textViewzhuti);

            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            //进行接口回调.
            int position = getAdapterPosition();
            if (listener != null) {
                listener.onItemClick(v,position);
            }

        }
    }
}
